using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Ports;
using System.Threading;



namespace ConsoleApplication1
{
    class Program
    {
        static Boolean _continue = true;
        static SerialPort sp;
        static string pong;

/*        public static void Read()
        {
            while (_continue)
            {
                try
                {
                    string message = sp.ReadLine();
                    Console.WriteLine(message);
                }
                catch (TimeoutException) { }
            }
        }
        */
        static void Main(string[] args)
        {
//            Thread readThread = new Thread(Read);
            string[] ports = new string[32];
            ports = scanPorts();
            foreach (string comPort in ports)
            {
                sp = new SerialPort();
                if (comPort == null)
                    break;
                sp.PortName = comPort;
//                sp.BaudRate = 9600;
//                sp.DataBits = 8;
//                sp.StopBits = 1;
//                sp.Parity = 0;
                sp.ReadTimeout = 2000;
                sp.WriteTimeout = 2000;
                sp.Open();
//                readThread.Start();
                sp.Write("p");
                pong = sp.ReadLine();
                if (pong[0] == 'o')
                {
                    Console.WriteLine( "Ambient Trust Cube responded on {0}", comPort);
                    editConfig( comPort );
                    break;
                }
                else
                {
                    Console.WriteLine( "Cube not on {0}.", comPort);
                }


                Console.WriteLine(pong);

            }
            Console.WriteLine("Press any key...");
            Console.ReadLine();

            sp.Close();


        }
        static string [] scanPorts()
        {
            int i = 0;
            string[] ports = new string[32];

            Console.WriteLine(" Available COM Ports Ambient Trust could potentially be on: ");

            foreach (string s in SerialPort.GetPortNames())
            {
                ports[i] = s;
                Console.WriteLine("   {0}", s);
                i++;
            }

            Console.WriteLine("  ");
            //Debug lines
            /*            foreach (string s in ports)
            {
                if (s != null)
                {
                    Console.WriteLine("     {0}", s);
                }
            }*/
            return ports;
        }
        static void editConfig(string comNumber)
        {
            int i = 0;
            int comLineNumber = 0;
            string [] cfgfile = new string[512];
            try
            {
                using (StreamReader sr = new StreamReader("serproxy/serproxy.cfg"))
                {
                    String line;
                    // Read and display lines from the file until the end of 
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null) // take in all of file
                    {
                        i++; //line number
                        if (line.StartsWith("serial_device1"))
                        {
                            comLineNumber = i;
                        }
                        cfgfile[i] = line;

                    }
  //                  cfgfile[comLineNumber] = "serial_device1={0}", 
                }

            }
            catch (Exception e)
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
        }


    }
}
